import Expression from "./interface/Expression";
import OnExpression from "./OnExpression";
import MetaData from "./MetaData";
import FieldExpression from "./impl/FieldExpression";
import BaseExpression from "./abstract/BaseExpression";
import StringUtils from "../utils/StringUtils";

/** join连表查询 */
class JoinExpression extends BaseExpression {
    // join的表
    private table: Expression;
    private alias: string = "";

    constructor(pa: MetaData, table: string | Expression, alias: string = "") {
        super(pa);
        this.table = typeof table === 'string' ? new FieldExpression(table as string) : table;
        this.alias = alias;
    }

    public toLocaSql(): string {
        return this.table.toLocaSql() + StringUtils.frontFill(this.alias, " ");
    }

    public on(left: string, right: string, inn: string = "=") {
        return new OnExpression(this.metadata, new FieldExpression(left, "on", this.metadata), new FieldExpression(right, this.metadata), inn);
    }
}

export = JoinExpression;